package com.jiaz.algorithm.threadPool;


import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StopWatch;

import java.util.*;
import java.util.concurrent.CountDownLatch;

/**
 * @author jiajiazi
 * @version 1.0
 * @date 2022/5/8 23:37
 */
@Slf4j
public class ItemOriginThreadScoreThread implements Runnable {
    private final int venueIndex;
    int n;
    double[] pgSimilaryOld;
    double[] pgSimilary;
    double[] taps;
    int tapi;
    List<Double[]>[] closeNod;
    double[] closeWeightSum;
    private CountDownLatch countDownLatch = null;

    public ItemOriginThreadScoreThread(int venueIndex, int n, double[] pgSimilaryOld, double[] pgSimilary, double[] taps, int tapi, List<Double[]>[] closeNod, double[] closeWeightSum, CountDownLatch countDownLatch) {
        this.venueIndex = venueIndex;
        this.n = n;
        this.pgSimilaryOld = pgSimilaryOld;
        this.pgSimilary = pgSimilary;
        this.taps = taps;
        this.tapi = tapi;
        this.closeNod = closeNod;
        this.closeWeightSum = closeWeightSum;
        this.countDownLatch = countDownLatch;
    }


    @Override
    public void run() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        //log.info("【多线程开始】 CF_Item 相似度得分计算");
        double a = 0.85;
        int nth = taps.length;
        //System.out.println("下标： " + n * tapi / nth + "--" + n * (tapi + 1) / nth);
        for (int i = n * tapi / nth; i < n * (tapi + 1) / nth; i++) {
            double d = 0;
            double self = i == venueIndex ? 1.0 : 0.0;
            for (Double[] doubles : closeNod[i]) {
                int j = doubles[0].intValue();
                d += pgSimilaryOld[j] * doubles[1] / closeWeightSum[j];
            }
            d = (1 - a) * self / n + a * d;
            double abs = Math.abs(d - pgSimilaryOld[i]);
            if (abs > taps[tapi]) {
                taps[tapi] = abs;
            }
            pgSimilary[i] = d;
        }
        countDownLatch.countDown();
        //stopWatch.stop();
        //log.info("【多线程结束】 CF_Item 相似度得分计算，耗时：{}ms", stopWatch.getTotalTimeMillis());

    }


}
